package com.easycms.web.system.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.easycms.framework.domain.ZtreeDto;
import com.easycms.framework.shiro.UserOwnInfo;
import com.easycms.framework.web.ActionResult;
import com.easycms.framework.web.BaseController;
import com.easycms.framework.web.RequestHelper;
import com.easycms.framework.domain.DataTableDto;
import com.easycms.framework.domain.ResultAdaptDto;
import com.easycms.web.system.domain.SysRole;
import com.easycms.web.system.dto.RoleButtonPermissionDto;
import com.easycms.web.system.service.SysRoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yushuo
 * @className
 * @descripton TODO
 * @date 2021/2/1 16:26
 **/
@Controller
@RequestMapping("/admin/sysrole")
public class SysRoleController extends BaseController {
    @Autowired
    private RequestHelper requestHelper;
    @Autowired
    private SysRoleService roleService;

    @RequiresPermissions("system:user:view")
    @GetMapping("/index")
    public ActionResult index(){

        return view();
    }

    @RequiresPermissions("system:user:view")
    @PostMapping("/getRoleList")
    public ActionResult getRoleList(DataTableDto dto){

        //, @RequestParam String roleName,@RequestParam String roleCode
        String roleName=requestHelper.getString("roleName","");
        String roleCode=requestHelper.getString("roleCode","");
        DataTableDto data=roleService.getRoleList(dto,roleName,roleCode);
        return json(data);
    }

    /**
    *
     * @author yushuo
     * @description //TODO
     * @date 9:34 2021/2/2
     * @param id
     * @return com.easycms.framework.web.ActionResult
    **/
    @RequiresPermissions(value = {"system:role:add","system:role:edit"},logical = Logical.OR)
    @GetMapping("/form")
    public ActionResult form(String id, ModelMap model){
        model.put("id",id);
        return view();
    }

    @RequiresPermissions(value = {"system:role:add","system:role:edit"},logical = Logical.OR)
    @PostMapping("/save")
    public ActionResult save(SysRole role){
        roleService.save(role,requestHelper.getUserOwnInfo());
        return ok("角色数据保存成功");
    }

    @RequiresPermissions(value = {"system:role:add","system:role:edit"},logical = Logical.OR)
    @GetMapping("/getModel")
    public ActionResult getModel(){
         Long id=requestHelper.getInt64("id",0L);
         if(id==0){
           return error("请求角色数据失败");
         }
         SysRole role=roleService.getModel(id);
         ResultAdaptDto result=new ResultAdaptDto();
         result.getData().put("model",role);
        return json(result);
    }

    @RequiresPermissions("system:role:delete")
    @GetMapping("/removeAll")
    public ActionResult removeAll(){

        String  ids=requestHelper.getString("ids","");
        if(StrUtil.isEmpty(ids)){
            return error("删除角色数据失败");
        }
        ResultAdaptDto result=new ResultAdaptDto();
        roleService.removeRole(ids);
        return ok("删除角色成功");
    }

    /**
    *
     * @author yushuo
     * @description //授权
     * @date 14:13 2021/2/22
     * @param []
     * @return com.easycms.framework.web.ActionResult
    **/
    @RequiresPermissions("system:role:permission")
    @GetMapping("/setPermission")
    public ActionResult setPermission(Model model){
        Long id=requestHelper.getInt64("id",0L);

        SysRole role=roleService.getModel(id);
        model.addAttribute("roleId",id);
        model.addAttribute("roleName",role.getRoleName());
        return view();
    }


    /**
    *
     * @author yushuo
     * @description //TODO
     * @date 14:15 2021/2/22
     * @param []
     * @return com.easycms.framework.web.ActionResult
    **/
    @RequiresPermissions("system:role:permission")
    @GetMapping("/getPermissions")
    public ActionResult getPermissions(){
        Long id=requestHelper.getInt64("id",0L);
        ResultAdaptDto result=new ResultAdaptDto();
        List<ZtreeDto> rolepermsList= roleService.getRoleMenuList(id);

        result.put("permsList",rolepermsList);
        return json(result);
    }

    /**
    *
     * @author yushuo
     * @description //保存权限
     * @date 15:17 2021/2/22
     * @param []
     * @return com.easycms.framework.web.ActionResult
    **/
    @RequiresPermissions("system:role:permission")
    @PostMapping("/savePermissions")
    public ActionResult savePermissions(){
        Long roleId=requestHelper.getInt64("roleId",0L);
        String permissions=requestHelper.getString("ids","");
        List<ZtreeDto> permissionsList=new ArrayList<>();
        if(!StrUtil.isEmpty(permissions)){
            permissionsList= JSONUtil.toList(permissions,ZtreeDto.class);
        }
        roleService.savePermissions(roleId,permissionsList,"yushuo");

        return ok("权限设置成功");
    }

    /**
     * @author yushuo
     * @description 获取前台按钮权限
     * @date 15:43 2021/2/23
     * @param []
     * @return com.easycms.framework.web.ActionResult
    **/
    @GetMapping("/getButtonPermission")
    public ActionResult getButtonPermission(){

        String author=requestHelper.getString("author","");
        if("easycms".equals(author)==false){
            return error("请求权限失败");
        }

        UserOwnInfo userOwnInfo=requestHelper.getUserOwnInfo();
        List<RoleButtonPermissionDto> buttonPermissionList=new ArrayList<>();
        if(!userOwnInfo.isAdmin()){
            buttonPermissionList= roleService.getButtonPermissionList(userOwnInfo.getRoleId());
        }

        ResultAdaptDto result=new ResultAdaptDto();
        result.put("isAdmin",userOwnInfo.isAdmin());
        result.put("buttonPermissions",buttonPermissionList);
        return json(result);
    }
}
